<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8">
    <meta name="keywords" content="typeScript学习, HTML,CSS,JavaScript,JQuery,React,Vue.js等">
    <meta name="description" content="别做一个Vue api调用师">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>typeScript学习 | zihao blog</title>
    <link rel="icon" type="image/png" href="/pride-blog/favicon.png">

    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/pride-blog/css/my.css">

    <script src="/pride-blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.3.0"><link rel="stylesheet" href="/pride-blog/css/prism-tomorrow.css" type="text/css"><!-- hexo-inject:begin --><!-- hexo-inject:end --></head>




<body>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/pride-blog/" class="waves-effect waves-light">
                    
                    <img src="/pride-blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">zihao blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/categories/CSS/" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/pride-blog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/pride-blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">zihao blog</div>
        <div class="logo-desc">
            
            别做一个Vue api调用师
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/categories/CSS/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/pride-blog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/pride-blog/medias/featureimages/7.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">typeScript学习</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/pride-blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/pride-blog/categories/typeScript/" class="post-category">
                                typeScript
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-06-24
                </div>
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="第一章-快速入门"><a href="#第一章-快速入门" class="headerlink" title="第一章 快速入门"></a>第一章 快速入门</h1><h2 id="0、TypeScript简介"><a href="#0、TypeScript简介" class="headerlink" title="0、TypeScript简介"></a>0、TypeScript简介</h2><ol>
<li>TypeScript是JavaScript的超集。</li>
<li>它对JS进行了扩展，向JS中引入了类型的概念，并添加了许多新的特性。</li>
<li>TS代码需要通过编译器编译为JS，然后再交由JS解析器执行。</li>
<li>TS完全兼容JS，换言之，任何的JS代码都可以直接当成JS使用。</li>
<li>相较于JS而言，TS拥有了静态类型，更加严格的语法，更强大的功能；TS可以在代码执行前就完成代码的检查，减小了运行时异常的出现的几率；TS代码可以编译为任意版本的JS代码，可有效解决不同JS运行环境的兼容问题；同样的功能，TS的代码量要大于JS，但由于TS的代码结构更加清晰，变量类型更加明确，在后期代码的维护中TS却远远胜于JS。</li>
</ol>
<h2 id="1、TypeScript-开发环境搭建"><a href="#1、TypeScript-开发环境搭建" class="headerlink" title="1、TypeScript 开发环境搭建"></a>1、TypeScript 开发环境搭建</h2><ol>
<li><p>下载Node.js</p>
<ul>
<li>64位：<a target="_blank" rel="noopener" href="https://nodejs.org/dist/v14.15.1/node-v14.15.1-x64.msi">https://nodejs.org/dist/v14.15.1/node-v14.15.1-x64.msi</a></li>
<li>32位：<a target="_blank" rel="noopener" href="https://nodejs.org/dist/v14.15.1/node-v14.15.1-x86.msi">https://nodejs.org/dist/v14.15.1/node-v14.15.1-x86.msi</a></li>
</ul>
</li>
<li><p>安装Node.js</p>
</li>
<li><p>使用npm全局安装typescript</p>
<ul>
<li>进入命令行</li>
<li>输入：npm i -g typescript</li>
</ul>
</li>
<li><p>创建一个ts文件</p>
</li>
<li><p>使用tsc对ts文件进行编译</p>
<ul>
<li><p>进入命令行</p>
</li>
<li><p>进入ts文件所在目录</p>
</li>
<li><p>执行命令：tsc xxx.ts</p>
</li>
</ul>
</li>
</ol>
<h2 id="2、基本类型"><a href="#2、基本类型" class="headerlink" title="2、基本类型"></a>2、基本类型</h2><ul>
<li><p>类型声明</p>
<ul>
<li><p>类型声明是TS非常重要的一个特点</p>
</li>
<li><p>通过类型声明可以指定TS中变量（参数、形参）的类型</p>
</li>
<li><p>指定类型后，当为变量赋值时，TS编译器会自动检查值是否符合类型声明，符合则赋值，否则报错</p>
</li>
<li><p>简而言之，类型声明给变量设置了类型，使得变量只能存储某种类型的值</p>
</li>
<li><h2 id="语法："><a href="#语法：" class="headerlink" title="语法："></a>语法：</h2></li>
</ul>
</li>
</ul>
<pre class=" language-typescript"><code class="language-typescript">      <span class="token keyword">let</span> 变量<span class="token punctuation">:</span> 类型<span class="token punctuation">;</span>

      <span class="token keyword">let</span> 变量<span class="token punctuation">:</span> 类型 <span class="token operator">=</span> 值<span class="token punctuation">;</span>

      <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span>参数<span class="token punctuation">:</span> 类型<span class="token punctuation">,</span> 参数<span class="token punctuation">:</span> 类型<span class="token punctuation">)</span><span class="token punctuation">:</span> 类型<span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
          <span class="token operator">...</span>
      <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
      <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* 自动类型判断

  + TS拥有自动的类型判断机制
  + 当对变量的声明和赋值是同时进行的，TS编译器会自动判断变量的类型
  + 所以如果你的变量的声明和赋值时同时进行的，可以省略掉类型声明

* 类型：

  |  类型   |       例子        |              描述              |
  | :-----: | :---------------: | :----------------------------: |
  | number  |    1, -33, 2.5    |            任意数字            |
  | string  | 'hi', "hi", `</span></span>hi<span class="token template-string"><span class="token string">` |           任意字符串           |
  | boolean |    true、false    |       布尔值true或false        |
  | 字面量  |      其本身       |  限制变量的值就是该字面量的值  |
  |   any   |         *         |            任意类型            |
  | unknown |         *         |         类型安全的any          |
  |  void   | 空值（undefined） |     没有值（或undefined）      |
  |  never  |      没有值       |          不能是任何值          |
  | object  |  &amp;#123;name:'孙悟空'&amp;#125;  |          任意的JS对象          |
  |  array  |      [1, 2, 3]      |           任意JS数组           |
  |  tuple  |       [4, 5]       | 元素，TS新增类型，固定长度数组 |
  |  enum   |    enum&amp;#123;A, B&amp;#125;     |       枚举，TS中新增类型       |

* number

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> decimal<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> hex<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token number">0xf00d</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> binary<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token number">0b1010</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> octal<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token number">0o744</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> big<span class="token punctuation">:</span> bigint <span class="token operator">=</span> 100n<span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* boolean

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> isDone<span class="token punctuation">:</span> <span class="token keyword">boolean</span> <span class="token operator">=</span> <span class="token keyword">false</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* string

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> color<span class="token punctuation">:</span> <span class="token keyword">string</span> <span class="token operator">=</span> <span class="token string">"blue"</span><span class="token punctuation">;</span>
    color <span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">;</span>

    <span class="token keyword">let</span> fullName<span class="token punctuation">:</span> <span class="token keyword">string</span> <span class="token operator">=</span> <span class="token template-string"><span class="token string">`Bob Bobbington`</span></span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> age<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token number">37</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> sentence<span class="token punctuation">:</span> <span class="token keyword">string</span> <span class="token operator">=</span> <span class="token template-string"><span class="token string">`Hello, my name is $&amp;#123;fullName&amp;#125;.

    I'll be $&amp;#123;age + 1&amp;#125; years old next month.`</span></span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* 字面量

  + 也可以使用字面量去指定变量的类型，通过字面量可以确定变量的取值范围

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> color<span class="token punctuation">:</span> <span class="token string">'red'</span> <span class="token operator">|</span> <span class="token string">'blue'</span> <span class="token operator">|</span> <span class="token string">'black'</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> num<span class="token punctuation">:</span> <span class="token number">1</span> <span class="token operator">|</span> <span class="token number">2</span> <span class="token operator">|</span> <span class="token number">3</span> <span class="token operator">|</span> <span class="token number">4</span> <span class="token operator">|</span> <span class="token number">5</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* any

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> d<span class="token punctuation">:</span> <span class="token keyword">any</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
    d <span class="token operator">=</span> <span class="token string">'hello'</span><span class="token punctuation">;</span>
    d <span class="token operator">=</span> <span class="token keyword">true</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* unknown

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> notSure<span class="token punctuation">:</span> unknown <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>
    notSure <span class="token operator">=</span> <span class="token string">'hello'</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* void

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> unusable<span class="token punctuation">:</span> <span class="token keyword">void</span> <span class="token operator">=</span> undefined<span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* never

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">function</span> <span class="token function">error</span><span class="token punctuation">(</span>message<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">:</span> never <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
      <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* object（没啥用）

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> obj<span class="token punctuation">:</span> object <span class="token operator">=</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span><span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* array

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> list<span class="token punctuation">:</span> <span class="token keyword">number</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> list<span class="token punctuation">:</span> <span class="token keyword">Array</span><span class="token operator">&lt;</span><span class="token keyword">number</span><span class="token operator">></span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* tuple

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">let</span> x<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">string</span><span class="token punctuation">,</span> <span class="token keyword">number</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"hello"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* enum

  - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
    <span class="token keyword">enum</span> Color <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
      Red<span class="token punctuation">,</span>
      Green<span class="token punctuation">,</span>
      Blue<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> c<span class="token punctuation">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>

    <span class="token keyword">enum</span> Color <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
      Red <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
      Green<span class="token punctuation">,</span>
      Blue<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> c<span class="token punctuation">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>

    <span class="token keyword">enum</span> Color <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
      Red <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span>
      Green <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span>
      Blue <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> c<span class="token punctuation">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span>
    <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* 类型断言

  + 有些情况下，变量的类型对于我们来说是很明确，但是TS编译器却并不清楚，此时，可以通过类型断言来告诉编译器变量的类型，断言有两种形式：

    - 第一种
      - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
        <span class="token keyword">let</span> someValue<span class="token punctuation">:</span> unknown <span class="token operator">=</span> <span class="token string">"this is a string"</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> strLength<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span>someValue as <span class="token keyword">string</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span>
        <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

    - 第二种
      - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
        <span class="token keyword">let</span> someValue<span class="token punctuation">:</span> unknown <span class="token operator">=</span> <span class="token string">"this is a string"</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> strLength<span class="token punctuation">:</span> <span class="token keyword">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">&lt;</span><span class="token keyword">string</span><span class="token operator">></span>someValue<span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span>
        <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`



## 3、编译选项

* 自动编译文件

  + 编译文件时，使用 -w 指令后，TS编译器会自动监视文件的变化，并在文件发生变化时对文件进行重新编译。

  + 示例：
    - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>powershell
      tsc xxx<span class="token punctuation">.</span>ts <span class="token operator">-</span>w
      <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

* 自动编译整个项目

  + 如果直接使用tsc指令，则可以自动将当前项目下的所有ts文件编译为js文件。

  + 但是能直接使用tsc命令的前提时，要先在项目根目录下创建一个ts的配置文件 tsconfig.json

  + tsconfig.json是一个JSON文件，添加配置文件后，只需只需 tsc 命令即可完成对整个项目的编译

  + 配置选项：

    - include

      - 定义希望被编译文件所在的目录

      - 默认值：["\*\*/\*"]

      - 示例：
        - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
          <span class="token string">"include"</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token string">"src/**/*"</span><span class="token punctuation">,</span> <span class="token string">"tests/**/*"</span><span class="token punctuation">]</span>
          <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

        - 上述示例中，所有src目录和tests目录下的文件都会被编译

    - exclude

      - 定义需要排除在外的目录

      - 默认值：["node_modules", "bower_components", "jspm_packages"]

      - 示例：
        - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
          <span class="token string">"exclude"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"./src/hello/**/*"</span><span class="token punctuation">]</span>
          <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

        - 上述示例中，src下hello目录下的文件都不会被编译

    - extends

      - 定义被继承的配置文件

      - 示例：
        - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
          <span class="token string">"extends"</span><span class="token punctuation">:</span> <span class="token string">"./configs/base"</span>
          <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

        - 上述示例中，当前配置文件中会自动包含config目录下base.json中的所有配置信息

    - files

      - 指定被编译文件的列表，只有需要编译的文件少时才会用到

      - 示例：
        - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
          <span class="token string">"files"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
              <span class="token string">"core.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"sys.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"types.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"scanner.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"parser.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"utilities.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"binder.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"checker.ts"</span><span class="token punctuation">,</span>
              <span class="token string">"tsc.ts"</span>
            <span class="token punctuation">]</span>
          <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

        - 列表中的文件都会被TS编译器所编译

      - compilerOptions

        - 编译选项是配置文件中非常重要也比较复杂的配置选项

        - 在compilerOptions中包含多个子选项，用来完成对编译的配置

          - 项目选项

            - target

              - 设置ts代码编译的目标版本

              - 可选值：

                - ES3（默认）、ES5、ES6/ES2015、ES7/ES2016、ES2017、ES2018、ES2019、ES2020、ESNext

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"target"</span><span class="token punctuation">:</span> <span class="token string">"ES6"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

                - 如上设置，我们所编写的ts代码将会被编译为ES6版本的js代码

            - lib

              - 指定代码运行时所包含的库（宿主环境）

              - 可选值：

                - ES5、ES6/ES2015、ES7/ES2016、ES2017、ES2018、ES2019、ES2020、ESNext、DOM、WebWorker、ScriptHost ......

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"target"</span><span class="token punctuation">:</span> <span class="token string">"ES6"</span><span class="token punctuation">,</span>
                      <span class="token string">"lib"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"ES6"</span><span class="token punctuation">,</span> <span class="token string">"DOM"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                      <span class="token string">"outDir"</span><span class="token punctuation">:</span> <span class="token string">"dist"</span><span class="token punctuation">,</span>
                      <span class="token string">"outFile"</span><span class="token punctuation">:</span> <span class="token string">"dist/aa.js"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

            - module

              - 设置编译后代码使用的模块化系统

              - 可选值：

                - CommonJS、UMD、AMD、System、ES2020、ESNext、None

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>typescript
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"module"</span><span class="token punctuation">:</span> <span class="token string">"CommonJS"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

            - outDir

              - 编译后文件的所在目录

              - 默认情况下，编译后的js文件会和ts文件位于相同的目录，设置outDir后可以改变编译后文件的位置

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"outDir"</span><span class="token punctuation">:</span> <span class="token string">"dist"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

                - 设置后编译后的js文件将会生成到dist目录

            - outFile

              - 将所有的文件编译为一个js文件

              - 默认会将所有的编写在全局作用域中的代码合并为一个js文件，如果module制定了None、System或AMD则会将模块一起合并到文件之中

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"outFile"</span><span class="token punctuation">:</span> <span class="token string">"dist/app.js"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

            - rootDir

              - 指定代码的根目录，默认情况下编译后文件的目录结构会以最长的公共目录为根目录，通过rootDir可以手动指定根目录

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"rootDir"</span><span class="token punctuation">:</span> <span class="token string">"./src"</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

            - allowJs

              - 是否对js文件编译

            - checkJs

              - 是否对js文件进行检查

              - 示例：
                - 

`</span></span><span class="token template-string"><span class="token string">``</span></span>json
                  <span class="token string">"compilerOptions"</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                      <span class="token string">"allowJs"</span><span class="token punctuation">:</span> <span class="token keyword">true</span><span class="token punctuation">,</span>
                      <span class="token string">"checkJs"</span><span class="token punctuation">:</span> <span class="token keyword">true</span>
                  <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                  <span class="token template-string"><span class="token string">``</span></span><span class="token template-string"><span class="token string">`

            - removeComments

              - 是否删除注释
              - 默认值：false

            - noEmit

              - 不对代码进行编译
              - 默认值：false

            - sourceMap

              - 是否生成sourceMap
              - 默认值：false


          - 严格检查

            - strict
              - 启用所有的严格检查，默认值为true，设置后相当于开启了所有的严格检查
            - alwaysStrict
              - 总是以严格模式对代码进行编译
            - noImplicitAny
              - 禁止隐式的any类型
            - noImplicitThis
              - 禁止类型不明确的this
            - strictBindCallApply
              - 严格检查bind、call和apply的参数列表
            - strictFunctionTypes
              - 严格检查函数的类型
            - strictNullChecks
              - 严格的空值检查
            - strictPropertyInitialization
              - 严格检查属性是否初始化

          - 额外检查

            - noFallthroughCasesInSwitch
              - 检查switch语句包含正确的break
            - noImplicitReturns
              - 检查函数没有隐式的返回值
            - noUnusedLocals
              - 检查未使用的局部变量
            - noUnusedParameters
              - 检查未使用的参数

          - 高级
            - allowUnreachableCode
              - 检查不可达代码

              - 可选值：
                - true，忽略不可达代码
                - false，不可达代码将引起错误

            - noEmitOnError

              - 有错误的情况下不进行编译
              - 默认值：false

## 4、webpack

* 通常情况下，实际开发中我们都需要使用构建工具对代码进行打包，TS同样也可以结合构建工具一起使用，下边以webpack为例介绍一下如何结合构建工具使用TS。

* 步骤：

  1. 初始化项目
     - 进入项目根目录，执行命令 `</span></span><span class="token template-string"><span class="token string">``</span></span> npm init <span class="token operator">-</span>y
</code></pre>
<pre><code>   - 主要作用：创建package.json文件</code></pre>
<ol start="2">
<li><p>下载构建工具</p>
<ul>
<li><p><code>npm i -D webpack webpack-cli webpack-dev-server typescript ts-loader clean-webpack-plugin</code></p>
<ul>
<li>共安装了7个包<ul>
<li>webpack<ul>
<li>构建工具webpack</li>
</ul>
</li>
<li>webpack-cli<ul>
<li>webpack的命令行工具</li>
</ul>
</li>
<li>webpack-dev-server<ul>
<li>webpack的开发服务器</li>
</ul>
</li>
<li>typescript<ul>
<li>ts编译器</li>
</ul>
</li>
<li>ts-loader<ul>
<li>ts加载器，用于在webpack中编译ts文件</li>
</ul>
</li>
<li>html-webpack-plugin<ul>
<li>webpack中html插件，用来自动创建html文件</li>
</ul>
</li>
<li>clean-webpack-plugin<ul>
<li>webpack中的清除插件，每次构建都会先清除目录</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><h2 id="根目录下创建webpack的配置文件webpack-config-js"><a href="#根目录下创建webpack的配置文件webpack-config-js" class="headerlink" title="根目录下创建webpack的配置文件webpack.config.js"></a>根目录下创建webpack的配置文件webpack.config.js</h2></li>
</ol>
<pre class=" language-javascript"><code class="language-javascript">       <span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"path"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token keyword">const</span> HtmlWebpackPlugin <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"html-webpack-plugin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       <span class="token keyword">const</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
           CleanWebpackPlugin
       <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"clean-webpack-plugin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

       module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
           optimization<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
               minimize<span class="token punctuation">:</span> <span class="token boolean">false</span> <span class="token comment" spellcheck="true">// 关闭代码压缩，可选</span>
           <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>

           entry<span class="token punctuation">:</span> <span class="token string">"./src/index.ts"</span><span class="token punctuation">,</span>

           devtool<span class="token punctuation">:</span> <span class="token string">"inline-source-map"</span><span class="token punctuation">,</span>

           devServer<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
               contentBase<span class="token punctuation">:</span> <span class="token string">'./dist'</span>
           <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>

           output<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
               path<span class="token punctuation">:</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">"dist"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
               filename<span class="token punctuation">:</span> <span class="token string">"bundle.js"</span><span class="token punctuation">,</span>
               environment<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                   arrowFunction<span class="token punctuation">:</span> <span class="token boolean">false</span> <span class="token comment" spellcheck="true">// 关闭webpack的箭头函数，可选</span>
               <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
           <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>

           resolve<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
               extensions<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">".ts"</span><span class="token punctuation">,</span> <span class="token string">".js"</span><span class="token punctuation">]</span>
           <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>

           module<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
               rules<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                   test<span class="token punctuation">:</span> <span class="token regex">/\.ts$/</span><span class="token punctuation">,</span>
                   use<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                       loader<span class="token punctuation">:</span> <span class="token string">"ts-loader"</span>
                   <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>
                   exclude<span class="token punctuation">:</span> <span class="token regex">/node_modules/</span>
               <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">]</span>
           <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>

           plugins<span class="token punctuation">:</span> <span class="token punctuation">[</span>
               <span class="token keyword">new</span> <span class="token class-name">CleanWebpackPlugin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
               <span class="token keyword">new</span> <span class="token class-name">HtmlWebpackPlugin</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                   title<span class="token punctuation">:</span> <span class="token string">'TS测试'</span>
               <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
           <span class="token punctuation">]</span>

       <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>
<ol start="4">
<li><h2 id="根目录下创建tsconfig-json，配置可以根据自己需要"><a href="#根目录下创建tsconfig-json，配置可以根据自己需要" class="headerlink" title="根目录下创建tsconfig.json，配置可以根据自己需要"></a>根目录下创建tsconfig.json，配置可以根据自己需要</h2></li>
</ol>
<pre class=" language-json"><code class="language-json">       &amp;#<span class="token number">123</span><span class="token punctuation">;</span>
           <span class="token property">"compilerOptions"</span><span class="token operator">:</span> &amp;#<span class="token number">123</span><span class="token punctuation">;</span>
               <span class="token property">"target"</span><span class="token operator">:</span> <span class="token string">"ES2015"</span><span class="token punctuation">,</span>
               <span class="token property">"module"</span><span class="token operator">:</span> <span class="token string">"ES2015"</span><span class="token punctuation">,</span>
               <span class="token property">"strict"</span><span class="token operator">:</span> <span class="token boolean">true</span>
           &amp;#<span class="token number">125</span><span class="token punctuation">;</span>
       &amp;#<span class="token number">125</span><span class="token punctuation">;</span>
       ```

  <span class="token number">5</span>. 修改package.json添加如下配置
     - 

```json
       &amp;#<span class="token number">123</span><span class="token punctuation">;</span>
         ...略...
         <span class="token property">"scripts"</span><span class="token operator">:</span> &amp;#<span class="token number">123</span><span class="token punctuation">;</span>
           <span class="token property">"test"</span><span class="token operator">:</span> <span class="token string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span><span class="token punctuation">,</span>
           <span class="token property">"build"</span><span class="token operator">:</span> <span class="token string">"webpack"</span><span class="token punctuation">,</span>
           <span class="token property">"start"</span><span class="token operator">:</span> <span class="token string">"webpack serve --open chrome.exe"</span>
         &amp;#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>
         ...略...
       &amp;#<span class="token number">125</span><span class="token punctuation">;</span>
       ```

  <span class="token number">6</span>. 在src下创建ts文件，并在并命令行执行```npm run build```对代码进行编译，或者执行```npm start```来启动开发服务器



## <span class="token number">5</span>、Babel

* 经过一系列的配置，使得TS和webpack已经结合到了一起，除了webpack，开发中还经常需要结合babel来对代码进行转换以使其可以兼容到更多的浏览器，在上述步骤的基础上，通过以下步骤再将babel引入到项目中。

  <span class="token number">1</span>. 安装依赖包：
     - ```npm i -D @babel/core @babel/preset-env babel-loader core-js
</code></pre>
<pre><code> - 共安装了4个包，分别是：
   - @babel/core
     - babel的核心工具
   - @babel/preset-env
     - babel的预定义环境
   - @babel-loader
     - babel在webpack中的加载器
   - core-js
     - core-js用来使老版本的浏览器支持新版ES语法</code></pre>
<ol start="2">
<li><p>修改webpack.config.js配置文件</p>
<ul>
<li><pre class=" language-javascript"><code class="language-javascript"><span class="token operator">...</span>略<span class="token operator">...</span>
module<span class="token punctuation">:</span> <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
    rules<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
            test<span class="token punctuation">:</span> <span class="token regex">/\.ts$/</span><span class="token punctuation">,</span>
            use<span class="token punctuation">:</span> <span class="token punctuation">[</span>
                <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                    loader<span class="token punctuation">:</span> <span class="token string">"babel-loader"</span><span class="token punctuation">,</span>
                    options<span class="token punctuation">:</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                        presets<span class="token punctuation">:</span> <span class="token punctuation">[</span>
                            <span class="token punctuation">[</span>
                                <span class="token string">"@babel/preset-env"</span><span class="token punctuation">,</span>
                                <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                                    <span class="token string">"targets"</span><span class="token punctuation">:</span><span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                                        <span class="token string">"chrome"</span><span class="token punctuation">:</span> <span class="token string">"58"</span><span class="token punctuation">,</span>
                                        <span class="token string">"ie"</span><span class="token punctuation">:</span> <span class="token string">"11"</span>
                                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>
                                    <span class="token string">"corejs"</span><span class="token punctuation">:</span><span class="token string">"3"</span><span class="token punctuation">,</span>
                                    <span class="token string">"useBuiltIns"</span><span class="token punctuation">:</span> <span class="token string">"usage"</span>
                                <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                            <span class="token punctuation">]</span>
                        <span class="token punctuation">]</span>
                    <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
                <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token punctuation">,</span>
                <span class="token operator">&amp;</span>#<span class="token number">123</span><span class="token punctuation">;</span>
                    loader<span class="token punctuation">:</span> <span class="token string">"ts-loader"</span><span class="token punctuation">,</span>

                <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            exclude<span class="token punctuation">:</span> <span class="token regex">/node_modules/</span>
        <span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
    <span class="token punctuation">]</span>
<span class="token operator">&amp;</span>#<span class="token number">125</span><span class="token punctuation">;</span>
<span class="token operator">...</span>略<span class="token operator">...</span></code></pre>
</li>
<li><p>如此一来，使用ts编译后的文件将会再次被babel处理，使得代码可以在大部分浏览器中直接使用，可以在配置选项的targets中指定要兼容的浏览器版本。</p>
</li>
</ul>
</li>
</ol>

                
            </div>
            <hr/>

            



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/pride-blog/libs/share/css/share.min.css">
<div id="article-share">

    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/pride-blog/2022/071030507.html">
                    <div class="card-image">
                        
                        
                        <img src="/pride-blog/medias/featureimages/16.jpg" class="responsive-img" alt="javaScript-闭包">
                        
                        <span class="card-title">javaScript-闭包</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-07-10
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/pride-blog/categories/javaScript/" class="post-category">
                                    javaScript
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/pride-blog/tags/javaScript/">
                        <span class="chip bg-color">+ javaScript</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/pride-blog/2022/062358377.html">
                    <div class="card-image">
                        
                        
                        <img src="/pride-blog/medias/featureimages/15.jpg" class="responsive-img" alt="vue3-toRef和toRefs">
                        
                        <span class="card-title">vue3-toRef和toRefs</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-06-23
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/pride-blog/categories/Vue3/" class="post-category">
                                    Vue3
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/pride-blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/pride-blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <span id="year">2021</span>
            <a href="/pride-blog/about" target="_blank">zihao xiong</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link ">














</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/pride-blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/pride-blog/libs/materialize/materialize.min.js"></script>
    <script src="/pride-blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/pride-blog/libs/aos/aos.js"></script>
    <script src="/pride-blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/pride-blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/pride-blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/pride-blog/libs/others/clicklove.js" async="async"></script>
    
    

    

    

    <!--腾讯兔小巢-->
    
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/pride-blog/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/pride-blog/libs/background/ribbon-refresh.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/pride-blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/pride-blog/libs/instantpage/instantpage.js" type="module"></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
    

</body>
<script src="./../source/js/cursor.js"></script>

</html>
